global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/df_totlsw.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {

qui do $code_dir/config/labeling_finalvars.do



***********************************************************************************************************************
* like final dataset.do: Build the final regression datasets
***********************************************************************************************************************

************************************
*** FINAL DATASET
************************************


use ${dataset_dir}/dep_vars/bvd_year_depvars.dta, clear
mmerge BvD year using ${dataset_dir}/dep_vars/bvd_year_list${weight_window}_${wtype}.dta, unmatched(none)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_country_multinational${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_gdpcap_manuf_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_gdpcap_totind_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_devgdp_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_wages_vaemp_manuf_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_wages_vaemp_totind_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD year using ${dataset_dir}/indep_vars/bvd_year_manufshare_sharesgdpweighted${weight_window}_${wtype}.dta, unmatched(master)
mmerge BvD using ${final_dir}/BvD_industry.dta, unmatched(master)
replace industry = "Other" if _m == 1
drop _m

* ensure missing_weights indicator is correct also for years without wages
bys BvD : egen mw = max(missing_weights_1995)
replace missing_weights_1995 = mw
drop mw


* Logaritihm and averages

*generate logs first 
foreach v in lsw hsw {
	gen `v'MPm_1995_a = ln(`v'MPm_ALL_1995_wtd )
	ren ln`v'MPm_ALL_1995_wtd ln`v'MPm_1995_a
	gen `v'MPt_1995_a = ln(`v'MPt_ALL_1995_wtd )
	ren ln`v'MPt_ALL_1995_wtd ln`v'MPt_1995_a
}
	
gen gdppcMPm_1995_a = ln(gdppcMPm_ALL_1995_wtd )
gen gdppcMPt_1995_a = ln(gdppcMPt_ALL_1995_wtd )
gen vaempMPm_1995_a = ln(vaempMPm_ALL_1995_wtd )
ren vaempMPm_ALL_1995_wtd lnvaempMPm_1995_a
gen vaempMPt_1995_a = ln(vaempMPt_ALL_1995_wtd )
ren vaempMPt_ALL_1995_wtd lnvaempMPt_1995_a


* Now, averages
foreach vv in lngdpgap { 
	ren `vv'_ALL_1995_wtd `vv'_1995_a
}
	
foreach v in lsw hsw vaemp {
	foreach vr in shr_home shr_foreign shr2_home shr2_foreign {
		cap gen `v'MPm_`vr'_1995_a = ln(`v'MPm_`vr'_1995_wtd)
	}
}
	
foreach vr in shr_home shr_foreign shr2_home shr2_foreign {
	gen gdppcMPm_`vr'_1995_a = ln(gdppcMPm_`vr'_1995_wtd)
}

foreach vv in lngdpgap {
	foreach vr in shr_home shr_foreign shr2_home shr2_foreign {
		ren `vv'_`vr'_1995_wtd `vv'_`vr'_1995_a
	}
}


*normalizing the shares
qui ds *_shr_home_1995_a
foreach vaar in `r(varlist)' {
	local vv = substr("`vaar'",1,strpos("`vaar'","_")-1)
	if "`vv'" == "lngdpgap" {
		continue
	}
	gen term_home_var = exp(`vv'_shr2_home_1995_a)/exp(`vv'_1995_a)*maxweight_1995
	gen term_foreign_var = exp(`vv'_shr2_foreign_1995_a)/exp(`vv'_1995_a)*(1-maxweight_1995)
	gen _term_home = term_home_var if year==1995
	gen _term_foreign = term_foreign_var if year==1995
	bys lse_id : egen term_home_fixed = max(_term_home)
	bys lse_id : egen term_foreign_fixed = max(_term_foreign)
	gen `vv'_shr4_home_1995_a = `vv'_shr2_home_1995_a*term_home_fixed
	gen `vv'_shr4_foreign_1995_a = `vv'_shr2_foreign_1995_a*term_foreign_fixed
	drop term_* _term_*
}


drop *_wtd

*merge in spillovers for the dependent variable
foreach xxx in auto95_bia {
	mmerge BvD year using ${dataset_dir}/spillovers/bvd_year_spillovers_`xxx'.dta, unmatched(master)
}

drop _m
cap drop *plcb30* *phar* *chem*
cap drop *1982*
cap drop *1980*
keep if missing_weights_1995 == 0 & year >= 1991 & maxweight_1995 < 1

*labeling
labelingvarsfinal "lsw"
labelingvarsfinal "hsw"
labelingvarsfinal "vaemp"
labelingvarsfinal "gdppc"
labelingvarsfinal "lngdpgap"

compress
save ${final_dir}/regression_dataset${weight_window}_${wtype}.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat